var t = require("../../api.js"), e = getApp(), a = !1, o = !1, s = 2;

Page({
    data: {
        status: -1,
        order_list: [],
        new_order:'',
        show_no_data_tip: !1,
        hide: 1,
        qrcode: "",
        refund_hide: 1,
        refund_qrcode: '',
        alert_hide:1,
        form_id:'',
        revoke_tpl: ''
    },
    onLoad: function(t) {
      wx.hideShareMenu()
        e.pageOnLoad(this, t);
        var r = this;
        a = !1, o = !1, s = 2, r.loadOrderList(t.status || -1), getCurrentPages().length < 2 && r.setData({
            show_index: !0
        });
       this.pay_no()
    },
    // 付款倒计时
    pay_no:function(){
        // 获取order_list
        var now_time = Date.parse(new Date())/1000;
        var order_list = this.data.order_list;
        var over_day = this.data.over_day;
        if(order_list && order_list.length>0){
          for(var i in order_list){
            if (order_list[i].is_pay == 0 && order_list[i].pay_type != 2){
              var s_time = over_day -(now_time - order_list[i].addtimestr);
              if(s_time>0){
                var H = parseInt(s_time / (60 * 60)) > 9 ? parseInt(s_time / (60 * 60)) :"0"+parseInt(s_time / (60 * 60));
                var I = parseInt((s_time % (60 * 60)) / 60) > 9 ? parseInt((s_time % (60 * 60)) / 60) : "0" + parseInt((s_time % (60 * 60)) / 60);
                var S = s_time % 60 > 9 ? s_time % 60 : '0'+s_time % 60;
                order_list[i].s_pay_time = H+":"+I+":"+S;
              }else{
                order_list[i].s_pay_time = '';
              }
            }
          }
          this.setData({
            order_list :order_list
          })
        }
      setTimeout(this.pay_no, 1000);
    },
    loadOrderList: function(a) {
        void 0 == a && (a = -1);
        var o = this;
        o.setData({
            status: a
        }), wx.showLoading({
            title: "正在加载",
            mask: !0
        }), e.request({
            url: t.order.list,
            data: {
                status: o.data.status
            },
            success: function(t) {
                0 == t.code && (o.setData({
                    order_list: t.data.list,
                    pay_type_list: t.data.pay_type_list,
                    over_day: t.data.over_day,
                    new_order:t.data.new_order,
                    share_image_1: t.data.share_image_1,
                    share_image_2: t.data.share_image_2,
                    share_image: t.data.share_image,
                    alert_hide: t.data.new_order && t.data.new_order.order ? 0 :1,
                    revoke_tpl: t.data.revoke_tpl
                }), wx.getStorageSync("item") && wx.removeStorageSync("item")), o.setData({
                    show_no_data_tip: o.data.order_list && 0 == o.data.order_list.length
                });
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    onReachBottom: function() {
        var r = this;
        o || a || (o = !0, e.request({
            url: t.order.list,
            data: {
                status: r.data.status,
                page: s
            },
            success: function(t) {
                if (0 == t.code) {
                    var e = r.data.order_list.concat(t.data.list);
                    r.setData({
                        order_list: e,
                        pay_type_list: t.data.pay_type_list
                    }), 0 == t.data.list.length && (a = !0);
                }
                s++;
            },
            complete: function() {
                o = !1;
            }
        }));
    },
    orderPay_1: function(t) {
        var e = this, a = e.data.pay_type_list;
        1 == a.length ? (wx.showLoading({
            title: "正在提交",
            mask: !0
        }), 0 == a[0].payment && e.WechatPay(t), 3 == a[0].payment && e.BalancePay(t)) : wx.showModal({
            title: "提示",
            content: "选择支付方式",
            cancelText: "余额支付",
            confirmText: "线上支付",
            success: function(a) {
                wx.showLoading({
                    title: "正在提交",
                    mask: !0
                }), a.confirm ? e.WechatPay(t) : a.cancel && e.BalancePay(t);
            }
        });
    },
    WechatPay: function(a) {
        e.request({
            url: t.order.pay_data,
            data: {
                order_id: a.currentTarget.dataset.id,
                pay_type: "WECHAT_PAY"
            },
            complete: function() {
                wx.hideLoading();
            },
            success: function(t) {
                0 == t.code && wx.requestPayment({
                    _res: t,
                    timeStamp: t.data.timeStamp,
                    nonceStr: t.data.nonceStr,
                    package: t.data.package,
                    signType: t.data.signType,
                    paySign: t.data.paySign,
                    success: function(t) {},
                    fail: function(t) {},
                    complete: function(t) {
                        "requestPayment:fail" != t.errMsg && "requestPayment:fail cancel" != t.errMsg ? wx.redirectTo({
                            url: "/pages/order/order?status=1"
                        }) : wx.showModal({
                            title: "提示",
                            content: "订单尚未支付",
                            showCancel: !1,
                            confirmText: "确认",
                            success: function(t) {
                                t.confirm && wx.redirectTo({
                                    url: "/pages/order/order?status=0"
                                });
                            }
                        });
                    }
                }), 1 == t.code && wx.showToast({
                    title: t.msg,
                    image: "/images/icon-warning.png"
                });
            }
        });
    },
    BalancePay: function(a) {
        e.request({
            url: t.order.pay_data,
            data: {
                order_id: a.currentTarget.dataset.id,
                pay_type: "BALANCE_PAY"
            },
            complete: function() {
                wx.hideLoading();
            },
            success: function(t) {
                0 == t.code && wx.redirectTo({
                    url: "/pages/order/order?status=1"
                }), 1 == t.code && wx.showModal({
                    title: "提示",
                    content: t.msg,
                    showCancel: !1
                });
            }
        });
    },
    orderRevokeMs: function(e) {
        var o = this;
        wx.showModal({
            title: "提示",
            content: "是否取消该订单？",
            cancelText: "否",
            confirmText: "是",
            success: function(t) {
                if (t.cancel) return !0;
                t.confirm && (wx.showLoading({
                    title: "操作中"
                }), getApp().request({
                    url: getApp().api.miaosha.order_revoke,
                    data: {
                        order_id: e.currentTarget.dataset.id
                    },
                    success: function(t) {
                        wx.hideLoading(), wx.showModal({
                            title: "提示",
                            content: t.msg,
                            showCancel: !1,
                            success: function(t) {
                                t.confirm && o.loadOrderList(o.data.status);
                            }
                        });
                    }
                }));
            }
        });
    },
    orderRevokeMessage: function (a){
      var that = this;
      if (typeof wx.requestSubscribeMessage === "function" && that.data.revoke_tpl) {
        var revoke_tpl = that.data.revoke_tpl;
        return wx.requestSubscribeMessage({
          tmplIds: [revoke_tpl],
          success: function (res) {
            if (revoke_tpl != '' && res[revoke_tpl] == "accept") {
              that.data.form_id = revoke_tpl
            }
          },
          fail: function (res) {
            
          },
          complete: function (res) {
            that.orderRevoke(a);
          }
        })
      }else {
        that.orderRevoke(a);
      }
    },
    orderRevoke: function(a) {
        var o = this;
        wx.showModal({
            title: "提示",
            content: "是否取消该订单？",
            cancelText: "否",
            confirmText: "是",
            success: function(s) {
                if (s.cancel) return !0;
                s.confirm &&  (wx.showLoading({
                    title: "操作中"
                }), e.request({
                    url: t.order.revoke,
                    data: {
                        order_id: a.currentTarget.dataset.id,
                        form_id: o.data.revoke_tpl
                    },
                    success: function(t) {
                        wx.hideLoading(), wx.showModal({
                            title: "提示",
                            content: t.msg,
                            showCancel: !1,
                            success: function(t) {
                                t.confirm && o.loadOrderList(o.data.status);
                            }
                        });
                    }
                }));
            }
        });
    },
    orderConfirm: function(a) {
        var o = this;
        wx.showModal({
            title: "提示",
            content: "是否确认已收到货？",
            cancelText: "否",
            confirmText: "是",
            success: function(s) {
                if (s.cancel) return !0;
                s.confirm && (wx.showLoading({
                    title: "操作中"
                }), e.request({
                    url: t.order.confirm,
                    data: {
                        order_id: a.currentTarget.dataset.id
                    },
                    success: function(t) {
                        wx.hideLoading(), wx.showToast({
                            title: t.msg
                        }), 0 == t.code && o.loadOrderList(3);
                    }
                }));
            }
        });
    },
    orderConfirmMs: function(e) {
        var o = this;
        wx.showModal({
            title: "提示",
            content: "是否确认已收到货？",
            cancelText: "否",
            confirmText: "是",
            success: function(t) {
                if (t.cancel) return !0;
                t.confirm && (wx.showLoading({
                    title: "操作中"
                }), getApp().request({
                    url: getApp().api.miaosha.confirm,
                    data: {
                        order_id: e.currentTarget.dataset.id
                    },
                    success: function(t) {
                        wx.hideLoading(), wx.showToast({
                            title: t.msg
                        }), 0 == t.code && o.loadOrderList(3);
                    }
                }));
            }
        });
    },
    orderQrcode: function(a) {
        var o = this, s = o.data.order_list, r = a.target.dataset.index;
        wx.showLoading({
            title: "正在加载",
            mask: !0
        }), o.data.order_list[r].offline_qrcode ? (o.setData({
            hide: 0,
            qrcode: o.data.order_list[r].offline_qrcode
        }), wx.hideLoading()) : e.request({
            url: t.order.get_qrcode,
            data: {
                order_no: s[r].order_no
            },
            success: function(t) {
                0 == t.code ? o.setData({
                    hide: 0,
                    qrcode: t.data.url
                }) : wx.showModal({
                    title: "提示",
                    content: t.msg
                });
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    orderQrcodeMs: function(t) {
        var e = this, o = e.data.order_list, a = t.target.dataset.index;
        wx.showLoading({
            title: "正在加载",
            mask: !0
        }), e.data.order_list[a].offline_qrcode ? (e.setData({
            hide: 0,
            qrcode: e.data.order_list[a].offline_qrcode
        }), wx.hideLoading()) : getApp().request({
            url: getApp().api.order.get_qrcode,
            data: {
                order_no: o[a].order_no
            },
            success: function(t) {
                0 == t.code ? e.setData({
                    hide: 0,
                    qrcode: t.data.url
                }) : wx.showModal({
                    title: "提示",
                    content: t.msg
                });
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    hide: function(t) {
        this.setData({
            hide: 1,
            refund_hide: 1
        });
    },
    alert_hide: function (t) {
      this.setData({
        alert_hide: 1
      });
    },
    onShow: function() {
      e.pageOnShow(this);
    },
    tabberToIndex:function(){
      wx.switchTab({ url:"/pages/index/index" })
    },
    onShareAppMessage: function () {
      if (this.data.new_order && this.data.new_order.order != null){
        var that =this;
        var user_info = wx.getStorageSync('user_info');
        return {
          path: "/pages/order-share/index?order_id=" + this.data.new_order.order.id,
          title: that.data.new_order.text ? that.data.new_order.text : "老板，我是" + user_info.nickname + "，刚在店里购买了商品，请接单！",
          success: function (a) {

          },
          imageUrl: that.data.share_image
        };
      }
    },
    refundQrcode: function(a) {
        var o = this, order_no = a.target.dataset.order_no;
        wx.showLoading({
            title: "正在加载",
            mask: !0
        }), e.request({
            url: t.order.get_refund_qrcode,
            data: {
                order_no: order_no
            },
            success: function(t) {
                0 == t.code ? o.setData({
                    refund_hide: 0,
                    refund_qrcode: t.data.url
                }) : wx.showModal({
                    title: "提示",
                    content: t.msg
                });
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
});